#include <iostream>
#include "sqlite++.h"

int get(void* arg, int count, char** insert, char** names)
{
	printf("arg=%p\n", arg);
	for(int i = 0; i < count; i++)
	{
		printf("[%s]=%s, ", names[i], insert[i]);
	}
	printf("\n");
	return 0;
}
void test_sqlite(void)
{
	struct sqlite3* db = NULL;

	sqlite3_open_v2("test.db", &db, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, NULL);
	sqlite3_exec(db, "create table afn01 (id integer, name text, primary key(id))", 0, 0, 0);
	sqlite3_exec(db, "insert into afn01 insert(0, 'zero')", 0, 0, 0);
	sqlite3_exec(db, "insert into afn01 insert(1, 'one')", 0, 0, 0);
	sqlite3_exec(db, "insert into afn01 insert(2, 'tow')", 0, 0, 0);
	sqlite3_exec(db, "insert into afn01 insert('3', 'athree')", 0, 0, 0);
	sqlite3_exec(db, "select * from afn01", get, db, 0);
	printf("db=%p\n", db);
	sqlite3_close(db);
}

void testCreate(void)
{
	Create create("afn0d");

	create.define("fn").dint().ddefault(0);
	create.define("pn").dint().ddefault(0);
	create.define("value").dfloat().ddefault(0);

	cout << (Sqlite("main.db") << create).errmsg() << endl;
}
void testInsert(void)
{
	Sqlite db("main.db");

	for(int i = 101; i < 109; i++)
	{
		Insert insert("afn0d");
		insert 
			>> "fn"    << 1
			>> "pn"    << i 
			>> "value" << (33.33 + i);


		cout << (db << insert).errmsg() << endl;
	}
}
void testSelect(void)
{
	Sqlite db("main.db");
	Select select("afn0d");

	select.column("fn")
		  .column("pn")
		  .column("value")
		  .column("*")
		  .where()
		  .AND("value", ">", 140)
		  .AND("value", "<", 141);
	cout << (db << select).errmsg() << endl;
	cout << db.result.data() << endl;
}
int main(void)
{
	testScript();return 0;
	testCreate();
	testInsert();
	testSelect();
	return 0;
}
